package com.wangce.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.ce.entity.MyErr;
import com.github.pagehelper.PageInfo;
import com.wangce.entity.*;
import com.wangce.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * ClassName: OrderController
 * Description:
 * date: 2021/12/3 10:19
 *
 * @author 凡人
 * @since JDK 1.8
 */
@Controller
public class OrderController {

    @Autowired
    OrderService service;

    /**
     * 主页面
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "index";
    }

    /**
     * 出库列表
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Dim d, @RequestParam(defaultValue = "1") Integer cur,
                       @RequestParam(defaultValue = "15")Integer size){
        PageInfo<Order> o = service.list(d,cur,size);
        model.addAttribute("list",o);
        model.addAttribute("d",d);
        return "orderList";
    }

    /**
     * 获取菜单
     * @return
     */
    @RequestMapping("getTree")
    @ResponseBody
    public List<Menu> getTree(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Menu> list = service.getTree();
        if(user!=null){
            list.get(0).setText(user.getUsername());
        }
        return list;
    }

    /**
     * 去登录
     * @return
     */
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 登陆
     * @param u
     * @return
     */
    @RequestMapping("login")
    public String login(HttpSession session,User u){
        User user = service.login(u);
        if(user !=null){
            session.setAttribute("user",user);
            return "redirect:index";
        }
        return "login";
    }

    /**
     * 去添加
     * @param model
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(Model model,Send s,Order o){
        long l = IdUtil.getSnowflake(31, 31).nextId();
        model.addAttribute("l",l);
        model.addAttribute("s",s);
        model.addAttribute("o",o);
        return "add";
    }

    /**
     * 添加
     * @param o
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public String add(Order o){
        Integer i = service.add(o);
        if(i>0){
            return "ok";
        }
        return "no";
    }

    /**
     * 上传附件
     * @param file
     * @return
     */
    @RequestMapping("upFile")
    @ResponseBody
    public MyErr upFile(MultipartFile file, Model model) throws IOException {
        if(file!=null && !file.isEmpty() && file.getSize() < 5*1024*1024){
            String name = file.getOriginalFilename();
            String suffx = name.substring(name.lastIndexOf("."));
            String newName = UUID.randomUUID() + suffx;
            file.transferTo(new File("d:/pic/"+newName));
            return new MyErr(200,"/pic/"+newName,null);
        }
        return new MyErr(500,"图片不能大于5M",null);
    }

    /**
     * 出货
     * @param o
     * @param model
     * @return
     */
    @RequestMapping("shipping")
    public String shipping(Order o,Model model){
        long oDetail = IdUtil.getSnowflake(31,31).nextId();
        model.addAttribute("o",o);
        model.addAttribute("oDetail",oDetail);
        return "shipping";
    }

    @RequestMapping("addSend")
    @ResponseBody
    public String addSend(Send s){
        Integer i= service.addSend(s);
        if(i>0){
            return "ok";
        }
        return "no";
    }
}
